﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace com.kmer.library.Helper
{
    public static class GetHelper
    {
        public static string HttpGetData(string URL)
        {
            var myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.Method = "GET";
            myRequest.ContentType = "text/html;charset=UTF-8";
            var myResponse = (HttpWebResponse)myRequest.GetResponse();
            var newStream = myResponse.GetResponseStream();
            var reader = new StreamReader(newStream, Encoding.UTF8);
            var content = reader.ReadToEnd();
            reader.Close();
            newStream.Close();
            myResponse.Close();
            return content;
        }
    }


    public static class PostHelper
    {
        public static string HttpPostData(string requestURI, string requestMethod, string postData, string ContentType)
        {
            var data = Encoding.GetEncoding("UTF-8").GetBytes(postData);
            var url = $"{requestURI}/{requestMethod}";
            var myRequest = (HttpWebRequest)WebRequest.Create(url);

            myRequest.Method = "POST";
            myRequest.ContentType = ContentType;
            myRequest.ContentLength = data.Length;
            var newStream = myRequest.GetRequestStream();

            newStream.Write(data, 0, data.Length);
            newStream.Close();

            var myResponse = (HttpWebResponse)myRequest.GetResponse();
            var reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            var content = reader.ReadToEnd();
            reader.Close();
            myResponse.Close();
            return content;
        }
    }

    public static class SoapHelper
    {
        public static string SoapPostData(string requestURI, string SoapAction, string postData, string ContentType)
        {
            var xml = new XmlDocument();
            xml.LoadXml(postData);
            var myRequest = (HttpWebRequest)WebRequest.Create(requestURI);

            myRequest.Headers.Add("SOAPAction", SoapAction);
            myRequest.ContentType = "text/xml;charset=\"utf-8\"";
            myRequest.Accept = "text/xml";
            myRequest.Method = "POST";
            //myRequest.Credentials = this.Credentials;

            var newStream = myRequest.GetRequestStream();
            xml.Save(newStream);
            newStream.Close();

            var myResponse = (HttpWebResponse)myRequest.GetResponse();
            var reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            var content = reader.ReadToEnd();
            reader.Close();
            myResponse.Close();
            return content;
        }
    }
}
